/*     */ package com.zimbra.qa.unittest.prov.soap;
/*     */ 
/*     */ import com.google.common.collect.Lists;
/*     */ import com.google.common.collect.Sets;
/*     */ import com.zimbra.common.soap.SoapTransport;
/*     */ import com.zimbra.common.util.Pair;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.Domain;
/*     */ import com.zimbra.cs.account.NamedEntry;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.qa.unittest.TestUtil;
/*     */ import com.zimbra.qa.unittest.prov.Verify;
/*     */ import com.zimbra.soap.account.message.CheckRightsRequest;
/*     */ import com.zimbra.soap.account.message.CheckRightsResponse;
/*     */ import com.zimbra.soap.account.message.GrantRightsRequest;
/*     */ import com.zimbra.soap.account.message.GrantRightsResponse;
/*     */ import com.zimbra.soap.account.type.AccountACEInfo;
/*     */ import com.zimbra.soap.account.type.CheckRightsRightInfo;
/*     */ import com.zimbra.soap.account.type.CheckRightsTargetInfo;
/*     */ import com.zimbra.soap.account.type.CheckRightsTargetSpec;
/*     */ import com.zimbra.soap.type.GranteeType;
/*     */ import com.zimbra.soap.type.TargetBy;
/*     */ import com.zimbra.soap.type.TargetType;
/*     */ import java.util.Collections;
/*     */ import java.util.List;
/*     */ import java.util.Set;
/*     */ import org.junit.AfterClass;
/*     */ import org.junit.Assert;
/*     */ import org.junit.BeforeClass;
/*     */ import org.junit.Test;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class TestCheckRights
/*     */   extends SoapTest
/*     */ {
/*     */   private static SoapProvTestUtil provUtil;
/*     */   private static Provisioning prov;
/*     */   private static Domain domain;
/*     */   
/*     */   @BeforeClass
/*     */   public static void init()
/*     */     throws Exception
/*     */   {
/*  60 */     provUtil = new SoapProvTestUtil();
/*  61 */     prov = provUtil.getProv();
/*  62 */     domain = provUtil.createDomain(baseDomainName());
/*     */   }
/*     */   
/*     */   @AfterClass
/*     */   public static void cleanup() throws Exception {
/*  67 */     Cleanup.deleteAll(new String[] { baseDomainName() });
/*     */   }
/*     */   
/*     */   private void grantRight(Account target, GranteeType granteeType, NamedEntry grantee, String right)
/*     */     throws Exception
/*     */   {
/*  73 */     SoapTransport transport = authUser(target.getName());
/*     */     
/*  75 */     GrantRightsRequest req = new GrantRightsRequest();
/*     */     
/*  77 */     AccountACEInfo ace = new AccountACEInfo(granteeType, right);
/*  78 */     ace.setZimbraId(grantee.getId());
/*  79 */     req.addAce(ace);
/*  80 */     GrantRightsResponse resp = (GrantRightsResponse)invokeJaxb(transport, req);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void basic() throws Exception {
/*  85 */     String right = "loginAs";
/*     */     
/*  87 */     Account acct = provUtil.createAccount(genAcctNameLocalPart("target"), domain);
/*  88 */     Account target1 = provUtil.createAccount(genAcctNameLocalPart("target1"), domain);
/*  89 */     Account target2 = provUtil.createAccount(genAcctNameLocalPart("target2"), domain);
/*     */     
/*     */ 
/*  92 */     grantRight(target1, GranteeType.usr, acct, right);
/*     */     
/*  94 */     SoapTransport transport = authUser(acct.getName());
/*     */     
/*  96 */     CheckRightsRequest req = new CheckRightsRequest();
/*     */     
/*  98 */     req.addTarget(new CheckRightsTargetSpec(TargetType.account, TargetBy.name, target1.getName(), Lists.newArrayList(new String[] { right })));
/*     */     
/* 100 */     req.addTarget(new CheckRightsTargetSpec(TargetType.account, TargetBy.name, target2.getName(), Lists.newArrayList(new String[] { right })));
/*     */     
/* 102 */     CheckRightsResponse resp = (CheckRightsResponse)invokeJaxb(transport, req);
/*     */     
/* 104 */     Set<String> target1Result = Sets.newHashSet();
/* 105 */     Set<String> target2Result = Sets.newHashSet();
/*     */     
/* 107 */     List<CheckRightsTargetInfo> targets = resp.getTargets();
/* 108 */     for (CheckRightsTargetInfo target : targets) {
/* 109 */       Assert.assertEquals(TargetType.account, target.getTargetType());
/* 110 */       Assert.assertEquals(TargetBy.name, target.getTargetBy());
/*     */       
/* 112 */       String key = target.getTargetKey();
/*     */       
/* 114 */       result = null;
/*     */       
/* 116 */       if (target1.getName().equals(key)) {
/* 117 */         Assert.assertEquals(Boolean.valueOf(true), Boolean.valueOf(target.getAllow()));
/* 118 */         result = target1Result;
/* 119 */       } else if (target2.getName().equals(key)) {
/* 120 */         Assert.assertEquals(Boolean.valueOf(false), Boolean.valueOf(target.getAllow()));
/* 121 */         result = target2Result;
/*     */       } else {
/* 123 */         Assert.fail();
/*     */       }
/*     */       
/* 126 */       List<CheckRightsRightInfo> rights = target.getRights();
/* 127 */       for (CheckRightsRightInfo rightInfo : rights) {
/* 128 */         result.add(rightInfo.getRight() + ":" + rightInfo.getAllow());
/*     */       }
/*     */     }
/*     */     
/*     */     Set<String> result;
/* 133 */     Verify.verifyEquals(Sets.newHashSet(new String[] { right + ":" + true }), target1Result);
/* 134 */     Verify.verifyEquals(Sets.newHashSet(new String[] { right + ":" + false }), target2Result);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void defaultPermission()
/*     */     throws Exception
/*     */   {
/* 141 */     Pair<String, Boolean> right1 = new Pair("invite", Boolean.TRUE);
/* 142 */     Pair<String, Boolean> right2 = new Pair("viewFreeBusy", Boolean.TRUE);
/* 143 */     Pair<String, Boolean> right3 = new Pair("loginAs", Boolean.FALSE);
/*     */     
/* 145 */     Account acct = provUtil.createAccount(genAcctNameLocalPart("acct"), domain);
/* 146 */     String targetKey = "not-exist@test.com";
/*     */     
/* 148 */     SoapTransport transport = authUser(acct.getName());
/*     */     
/* 150 */     CheckRightsRequest req = new CheckRightsRequest();
/*     */     
/* 152 */     CheckRightsTargetSpec targetSpec = new CheckRightsTargetSpec(TargetType.account, TargetBy.name, targetKey, Lists.newArrayList(new String[] { (String)right1.getFirst(), (String)right2.getFirst(), (String)right3.getFirst() }));
/*     */     
/*     */ 
/*     */ 
/* 156 */     req.addTarget(targetSpec);
/*     */     
/* 158 */     CheckRightsResponse resp = (CheckRightsResponse)invokeJaxb(transport, req);
/*     */     
/* 160 */     List<CheckRightsTargetInfo> targets = resp.getTargets();
/* 161 */     for (CheckRightsTargetInfo target : targets)
/*     */     {
/* 163 */       Assert.assertEquals(TargetType.account, target.getTargetType());
/* 164 */       Assert.assertEquals(TargetBy.name, target.getTargetBy());
/* 165 */       Assert.assertEquals(targetKey, target.getTargetKey());
/* 166 */       Assert.assertEquals(Boolean.valueOf((((Boolean)right1.getSecond()).booleanValue()) && (((Boolean)right2.getSecond()).booleanValue()) && (((Boolean)right3.getSecond()).booleanValue())), Boolean.valueOf(target.getAllow()));
/*     */       
/*     */ 
/* 169 */       Set<String> actual = Sets.newHashSet();
/* 170 */       List<CheckRightsRightInfo> rights = target.getRights();
/* 171 */       for (CheckRightsRightInfo rightInfo : rights) {
/* 172 */         actual.add(rightInfo.getRight() + ":" + rightInfo.getAllow());
/*     */       }
/* 174 */       Verify.verifyEquals(Sets.newHashSet(new String[] { (String)right1.getFirst() + ":" + right1.getSecond(), (String)right2.getFirst() + ":" + right2.getSecond(), (String)right3.getFirst() + ":" + right3.getSecond() }), actual);
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   @Test
/*     */   public void delegatedSendRight()
/*     */     throws Exception
/*     */   {
/* 184 */     String right = "sendAs";
/*     */     
/* 186 */     Account acct = provUtil.createAccount(genAcctNameLocalPart("target"), domain);
/*     */     
/* 188 */     Account targetAcct = provUtil.createAccount(genAcctNameLocalPart("target-acct"), domain);
/* 189 */     String alias1 = TestUtil.getAddress(genAcctNameLocalPart("target-acct-alias-1"), domain.getName());
/* 190 */     String alias2 = TestUtil.getAddress(genAcctNameLocalPart("target-acct-alias-2"), domain.getName());
/* 191 */     prov.addAlias(targetAcct, alias1);
/* 192 */     prov.addAlias(targetAcct, alias2);
/* 193 */     prov.modifyAttrs(targetAcct, Collections.singletonMap("zimbraPrefAllowAddressForDelegatedSender", alias1));
/*     */     
/*     */ 
/* 196 */     grantRight(targetAcct, GranteeType.usr, acct, right);
/*     */     
/* 198 */     SoapTransport transport = authUser(acct.getName());
/*     */     
/* 200 */     CheckRightsRequest req = new CheckRightsRequest();
/*     */     
/* 202 */     req.addTarget(new CheckRightsTargetSpec(TargetType.account, TargetBy.name, targetAcct.getName(), Lists.newArrayList(new String[] { right })));
/*     */     
/* 204 */     req.addTarget(new CheckRightsTargetSpec(TargetType.account, TargetBy.name, alias1, Lists.newArrayList(new String[] { right })));
/*     */     
/* 206 */     req.addTarget(new CheckRightsTargetSpec(TargetType.account, TargetBy.name, alias2, Lists.newArrayList(new String[] { right })));
/*     */     
/* 208 */     CheckRightsResponse resp = (CheckRightsResponse)invokeJaxb(transport, req);
/*     */     
/* 210 */     Set<String> primaryMailResult = Sets.newHashSet();
/* 211 */     Set<String> alias1Result = Sets.newHashSet();
/* 212 */     Set<String> alias2Result = Sets.newHashSet();
/*     */     
/* 214 */     List<CheckRightsTargetInfo> targets = resp.getTargets();
/* 215 */     for (CheckRightsTargetInfo target : targets) {
/* 216 */       Assert.assertEquals(TargetType.account, target.getTargetType());
/* 217 */       Assert.assertEquals(TargetBy.name, target.getTargetBy());
/*     */       
/* 219 */       String key = target.getTargetKey();
/*     */       
/* 221 */       result = null;
/*     */       
/* 223 */       if (targetAcct.getName().equals(key)) {
/* 224 */         Assert.assertEquals(Boolean.valueOf(false), Boolean.valueOf(target.getAllow()));
/* 225 */         result = primaryMailResult;
/* 226 */       } else if (alias1.equals(key)) {
/* 227 */         Assert.assertEquals(Boolean.valueOf(true), Boolean.valueOf(target.getAllow()));
/* 228 */         result = alias1Result;
/* 229 */       } else if (alias2.equals(key)) {
/* 230 */         Assert.assertEquals(Boolean.valueOf(false), Boolean.valueOf(target.getAllow()));
/* 231 */         result = alias2Result;
/*     */       } else {
/* 233 */         Assert.fail();
/*     */       }
/*     */       
/* 236 */       List<CheckRightsRightInfo> rights = target.getRights();
/* 237 */       for (CheckRightsRightInfo rightInfo : rights) {
/* 238 */         result.add(rightInfo.getRight() + ":" + rightInfo.getAllow());
/*     */       }
/*     */     }
/*     */     
/*     */     Set<String> result;
/* 243 */     Verify.verifyEquals(Sets.newHashSet(new String[] { right + ":" + false }), primaryMailResult);
/* 244 */     Verify.verifyEquals(Sets.newHashSet(new String[] { right + ":" + true }), alias1Result);
/* 245 */     Verify.verifyEquals(Sets.newHashSet(new String[] { right + ":" + false }), alias2Result);
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/qa/unittest/prov/soap/TestCheckRights.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */